package com.wandian.news.mvvm.lifetool.food;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.adapter.LifeFoodRecycleAdapter;
import com.wandian.news.databinding.NewsActivityLifeFoodBinding;
import com.wandian.news.databinding.NewsActivityMenuBinding;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 食材activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NEWS_FOOD)
public class LifeFoodActivity extends BaseMVVMActivity<LifeFoodViewModel> implements BaseQuickAdapter.RequestLoadMoreListener {
    private NewsActivityLifeFoodBinding dataBinding;
    private LifeFoodRecycleAdapter adapter;
    private int page =1;
    private String searchName="";
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_life_food);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        initRecycle();

    }

    private void getData(boolean isReflsh){
        if(isReflsh){
            page =1;
            if(!UIUtils.isEmpty(adapter) && !UIUtils.isEmpty(adapter.getData())){
                adapter.getData().clear();
                adapter.notifyDataSetChanged();
            }
        }else {
            page++;
        }
        if(UIUtils.isEmpty(searchName))return;
        mViewModel.getData(page,isReflsh,searchName);
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("食材成分");
        mViewModel.dataBean.observe(this,dataBean->{
            if(!UIUtils.isEmpty(dataBean) && !UIUtils.isEmpty(dataBean.getNewslist())){
                if(mViewModel.isReFlash){
                    adapter.setNewData(dataBean.getNewslist());
                }else {
                    adapter.addData(dataBean.getNewslist());
                    adapter.loadMoreComplete();
                }
                if(dataBean.getNewslist().size()<20)adapter.setEnableLoadMore(false);
            }

        });
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());

        dataBinding.tvSearch.setOnClickListener(v -> {
            finishhideSoftBoard();
           if(!UIUtils.isEmpty(dataBinding.etSearch.getText())){
               searchName = dataBinding.etSearch.getText().toString();
               getData(true);
           }else {
               ToastUtil.show("请输入需要查询的食材");
           }
        });
    }

    private void initRecycle(){
        adapter = new LifeFoodRecycleAdapter(R.layout.news_item_life_food_style);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        dataBinding.recycler.setLayoutManager(layoutManager);
        dataBinding.recycler.setAdapter(adapter);
        adapter.setOnLoadMoreListener(this, dataBinding.recycler);

    }

    @Override
    public void onLoadMoreRequested() {
        getData(false);
    }

    @Override
    protected void onDestroy() {
        finishhideSoftBoard();
        super.onDestroy();
    }
}
